package singleton;

// 懒汉式
// double check + volatile 保证单例线程安全
class LazyDoubleCheckSingleton {
    // volatile防止指令重排序
    private volatile static LazyDoubleCheckSingleton instance = null;

    private LazyDoubleCheckSingleton() {
    }

    // 双重判断机制，在单例对象不存在的时候再去加锁
    public static LazyDoubleCheckSingleton getInstance() {
        if (instance == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (instance == null) {
                    instance = new LazyDoubleCheckSingleton();
                }
            }
        }
        return instance;
    }
}
